package vista;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Ventana extends JFrame implements ActionListener{
	
	//Atributos de la ventana
	private String titulo;
	private String fondo;
	private JPanel panelPrincipal;
	private int ancho;
	private int alto;
	private int posx;
	private int posy;
	
	/**
	 * Constructor de la clase
	 */
	public Ventana(){
		configurarVentana();
		eventosVentana();
		eventosMouse();
		eventosTeclado();
	}
	
	/**
	 * Datos de configuracion de la ventana por defecto
	 */
	public void configurarVentana(){
		ancho = 700;
		alto  = 500;
		posx  = 100;
		posy  = 100;
		titulo = " .:: Titulo de la Ventana ::. ";
		panelPrincipal = new JPanel();
		this.add(panelPrincipal);
		this.setBounds(posx, posy, ancho, alto);
		this.setTitle(titulo);
		this.setResizable(false);
	}
	
	/**
	 * Metodo sobrecargado para configurar la ventana 
	 */
	public void configurarVentana(int ancho, int alto, int posx, int posy, String titulo){
		this.ancho = ancho;
		this.alto  = alto;
		this.posx  = posx;
		this.posy  = posy;
		this.titulo = titulo;
		this.setBounds(posx, posy, ancho, alto);
		this.setTitle(titulo);
	}
	
	/**
	 * Metodo sobrecargado para configurar la ventana 
	 */
	public void cambiarTitulo(String titulo){
		this.titulo = titulo;
		this.setTitle(titulo);
		this.update(getGraphics());
	}
	
	/**
	 *  Metodo que maneja los eventos de la ventana
	 */
	private void eventosVentana() {
		// Manejo de eventos de la ventana
		this.addWindowListener(new WindowAdapter() {
			// Cerrando ventana
			public void windowClosing(WindowEvent e) {
				System.out.println("Se ha cerrado la ventana");
				System.exit(0);
			}

			// Ventana abierta
			public void windowOpened(WindowEvent e) {
				System.out.println("Ventana abierta");
			}

			// Ventana activa
			public void windowActivated(WindowEvent e) {
				System.out.println("Ventana activa");
			}

			// Ventana inactiva
			public void windowDeactivated(WindowEvent e) {
				System.out.println("Ventana inactiva");
			}

			// Cuando se minimiza la ventana
			public void windowIconified(WindowEvent e) {
				System.out.println("Ventana minimizada");
			}

			// Cuando se maximiza la ventana
			public void windowDeiconified(WindowEvent e) {
				System.out.println("Ventana restaurada");
			}
		});

	}
	
	private void eventosMouse(){
		this.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e){
				System.out.println("Se ha hecho clic en x=" + e.getX() + ", y=" + e.getY());
			}
		});
	}
	
	private void eventosTeclado(){
		this.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e){
				System.out.println("Se ha pulsado la tecla " + e.getKeyChar());
				e.consume();
			}
			public void keyPressed(KeyEvent e){
				System.out.println("Se ha pulsado la tecla x " + e.getKeyChar());
				e.consume();
			}
		});
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		
		System.out.println("Evento " + e.getSource().toString());
		
	}

	/**
	 * Muestra la ventana
	 */
	public void mostrarVentana(){
		this.setVisible(true);
	}
	
	public void cambiarVentana(Ventana v){
		this.dispose();
		Ventana ventana = v;
		v.mostrarVentana();
	}

}
